<template>
  <div>
    <div class="head">
      <el-input placeholder="请输入会员名" v-model="searchQuery" style="width: 300px;"></el-input>
      <el-button type="primary" @click="search">查询</el-button>
      <el-button type="primary" @click="handleAdd">添加</el-button>
    </div>
    <el-table :data="tableData">
      <el-table-column prop="username" label="会员名"></el-table-column>
      <el-table-column prop="teach" label="教练"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="sex" label="性别"></el-table-column>
      <el-table-column prop="phone" label="电话"></el-table-column>
      <el-table-column prop="address" label="住址"></el-table-column>
      <el-table-column prop="height" label="身高"></el-table-column>
      <el-table-column prop="weight" label="体重"></el-table-column>
      <el-table-column prop="lastcheck" label="到期时间"></el-table-column>
      <el-table-column prop="type" label="类型"></el-table-column>
      <el-table-column prop="description" label="客户描述"></el-table-column>
      <el-table-column label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="查询结果" :visible.sync="dialogFormVisible">
      <el-table :data="searchResults">
        <el-table-column prop="username" label="名字"></el-table-column>
        <el-table-column prop="teach" label="教练"></el-table-column>
        <el-table-column prop="type" label="会员类型"></el-table-column>
        <el-table-column prop="lastcheck" label="到期时间"></el-table-column>
        <el-table-column prop="description" label="训练描述"></el-table-column>
        <el-table-column prop="address" label="操作" width="150">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">关闭</el-button>
      </div>
    </el-dialog>

    <el-dialog title="会员编辑" :visible.sync="editDialogVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <div class="bookdetail">
          <el-form-item label="名字" :label-width="formLabelWidth" prop="username">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="教练" :label-width="formLabelWidth" prop="teach">
            <el-input v-model="form.teach" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth" prop="teach">
            <el-input v-model="form.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="身高" :label-width="formLabelWidth" prop="height">
            <el-input v-model="form.height" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="体重" :label-width="formLabelWidth" prop="weight">
            <el-input v-model="form.weight" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth" prop="phone">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址" :label-width="formLabelWidth" prop="address">
            <el-input v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
          <div class="lasttwo">
            <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="检查时间" :label-width="formLabelWidth" prop="lastcheck">
              <el-date-picker
                v-model="form.lastcheck"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="类型" :label-width="formLabelWidth" prop="type">
              <el-select v-model="form.type" placeholder="请选择类型">
                <el-option label="普通月卡" value="普通月卡"></el-option>
                <el-option label="普通季卡" value="普通季卡"></el-option>
                <el-option label="普通年卡" value="普通年卡"></el-option>
                <el-option label="白金月卡" value="白金月卡"></el-option>
                <el-option label="白金季卡" value="白金季卡"></el-option>
                <el-option label="白金年卡" value="白金年卡"></el-option>
                <el-option label="至尊" value="至尊"></el-option>
              </el-select>
            </el-form-item>
          </div>
          <el-form-item label="描述" :label-width="formLabelWidth" prop="description">
            <el-input type="textarea" v-model="form.description" autocomplete="off"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="添加会员" :visible.sync="addDialogVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <div class="bookdetail">
          <el-form-item label="名字" :label-width="formLabelWidth" prop="username">
            <el-input v-model="form.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="教练" :label-width="formLabelWidth" prop="teach">
            <el-input v-model="form.teach" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth" prop="teach">
            <el-input v-model="form.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="身高" :label-width="formLabelWidth" prop="height">
            <el-input v-model="form.height" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="体重" :label-width="formLabelWidth" prop="weight">
            <el-input v-model="form.weight" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth" prop="phone">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址" :label-width="formLabelWidth" prop="address">
            <el-input v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
          <div class="lasttwo">
            <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="到期时间" :label-width="formLabelWidth" prop="lastcheck">
              <el-date-picker
                v-model="form.lastcheck"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="类型" :label-width="formLabelWidth" prop="type">
              <el-select v-model="form.type" placeholder="请选择类型">
                <el-option label="普通月卡" value="普通月卡"></el-option>
                <el-option label="普通季卡" value="普通季卡"></el-option>
                <el-option label="普通年卡" value="普通年卡"></el-option>
                <el-option label="白金月卡" value="白金月卡"></el-option>
                <el-option label="白金季卡" value="白金季卡"></el-option>
                <el-option label="白金年卡" value="白金年卡"></el-option>
                <el-option label="至尊" value="至尊"></el-option>
              </el-select>
            </el-form-item>
          </div>
          <el-form-item label="描述" :label-width="formLabelWidth" prop="description">
            <el-input type="textarea" v-model="form.description" autocomplete="off"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="addBook">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      editDialogVisible: false,
      addDialogVisible: false,
      rules: {
        username: [
          { required: true, message: "请输入会员名", trigger: "blur" }
        ],
        teach: [{ required: true, message: "请输入教练", trigger: "blur" }],
        type: [{ required: true, message: "请选择类型", trigger: "change" }],
        age: [{ required: true, message: "请输入年龄", trigger: "change" }],
        description: [
          { required: true, message: "请输入描述", trigger: "blur" }
        ],
        sex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        phone: [{ required: true, message: "请输入电话", trigger: "blur" }],
        weight: [{ required: true, message: "请输入体重", trigger: "blur" }],
        height: [{ required: true, message: "请输入身高", trigger: "blur" }],
        lastcheck: [{ required: true, message: "请输入身高", trigger: "blur" }],
        address: [{ required: true, message: "请输入地址", trigger: "blur" }]
      },
      tableData: [],
      searchResults: [],
      form: {
        id: "",
        username: "",
        age: "",
        description: "",
        type: "",
        teach: "",
        sex: "",
        phone: "",
        weight: "",
        height: "",
        address: "",
        lastcheck: ""
      },
      formLabelWidth: "120px",
      searchQuery: ""
    };
  },
  methods: {
    handleEdit(row) {
      this.editDialogVisible = true;
      this.resetForm();
      this.form.id = row.id;
      this.form.age = row.age;
      this.form.username = row.username;
      this.form.teach = row.teach;
      this.form.type = row.type;
      this.form.description = row.description;
      this.form.sex = row.sex;
      this.form.phone = row.phone;
      this.form.weight = row.weight;
      this.form.height = row.height;
      this.form.address = row.address;
       this.form.lastcheck = row.lastcheck;
    },

    handleAdd() {
      this.addDialogVisible = true;
      this.resetForm();
    },

    handleDelete(row) {
      // console.log(row.id);
      this.$axios
        .delete(`http://47.108.248.93:999/users/${row.id}`, {
          headers: { token: localStorage.getItem("token") }
        })
        .then(res => {
          this.$message({
            message: res.data.msg,
            type: "success"
          });
          this.tableData = this.tableData.filter(item => item.id !== row.id);
        });
      this.resetForm();
    },

    handleCancel() {
      this.dialogFormVisible = false;
      this.addDialogVisible = false;
      this.editDialogVisible = false;
      this.resetForm();
    },

    handleSave() {
      this.$refs.form.validate(valid => {
        // console.log(this.form);
        if (valid) {
          this.$axios
            .put("http://47.108.248.93:999/users", this.form, {
              headers: { token: localStorage.getItem("token") }
            })
            .then(res => {
              this.$message({
                message: res.data.msg,
                type: "success"
              });
              this.editDialogVisible = false;
              this.getAll();
            });
        }
      });
      this.resetForm();
    },
    search() {
      this.searchResults = this.tableData.filter(item =>
        item.username.includes(this.searchQuery)
      );
      this.dialogFormVisible = true;
    },

    addBook() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$axios
            .post("http://47.108.248.93:999/users", this.form, {
              headers: { token: localStorage.getItem("token") }
            })
            .then(res => {
              this.$message({
                message: res.data.msg,
                type: "success"
              });
              this.addDialogVisible = false;
              this.getAll();
            });
        }
      });
    },
    resetForm() {
      this.form = {
        username: "",
        teach: "",
        type: "",
        description: "",
        sex: "",
        phone: "",
        weight: "",
        height: "",
        address: "",
        lastcheck: ""
      };
    },
    getAll() {
      const localtoken = localStorage.getItem("token");
      this.$axios
        .get("http://47.108.248.93:999/users", {
          headers: {
            token: localtoken
          }
        })
        .then(res => {
          this.tableData = res.data.data;
        });
    }
  },
  mounted() {
    const localtoken = localStorage.getItem("token");
    this.$axios
      .get("http://47.108.248.93:999/users", {
        headers: {
          token: localtoken
        }
      })
      .then(res => {
        this.tableData = res.data.data;
      });
  }
};
</script>

<style scoped>
.head {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.head .el-input {
  margin-right: 10px;
}

.head .el-button {
  margin-right: 10px;
}

.el-table {
  width: 100%;
  margin-bottom: 20px;
}

.el-dialog__body {
  padding-bottom: 0;
}

.bookdetail {
  width: 100%;
}

.bookdetail .el-form-item {
  margin-bottom: 20px;
}

.lasttwo {
  display: flex;
  justify-content: center;
  align-items: center;
}

.dialog-footer {
  text-align: right;
}
</style>
